/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.panels;

import java.awt.Container;
import java.awt.Window;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.event.ChangeEvent;
import researchgrants.parts.LoggedData.panels.events.ChangeEventHandler;
import javax.swing.event.ChangeListener;
import researchgrants.parts.LoggedData.panels.events.SaveValidationEvent;
import researchgrants.parts.LoggedData.panels.events.SaveValidationHandler;
import researchgrants.parts.LoggedData.panels.events.SaveValidationListener;

/**
 *
 * @author DOStudent1
 */
public abstract class Panels implements ChangeEventHandler, SaveValidationHandler {

    // These fields are optional but must be set for objects requiring parents
    // However, for future enhancements, all usages should initialize it
    private Window parentWindow;
    private boolean hasParent=false;
    private Container parentContainer;
    
    public Container getParentContainer() {
        return parentContainer;
    }

    public Window getParentWindow() {
        return parentWindow;
    }

    public boolean isParentInitialized() {
        return hasParent;
    }

    public void setParent(Window parentWindow, Container parentContainer) {
        this.parentWindow = parentWindow;
        this.parentContainer = parentContainer;
        hasParent = true;
    }

    // to facilitate mandatory fields (or sub fields)
    public abstract boolean isEmpty();

    public abstract boolean isValid();
    
    private Collection<ChangeListener> changeEventListeners = new LinkedList<ChangeListener>();

    @Override
    public void addChangeListener(ChangeListener changeEventListener) {
        changeEventListeners.add(changeEventListener);
    }

    protected void notifyAllChangeListeners(ChangeEvent eNew) {
        for (Iterator<ChangeListener> it = changeEventListeners.iterator(); it.hasNext();) {
            ChangeListener changeEventListener = it.next();
            changeEventListener.stateChanged(eNew);
        }
    }

    private Collection<SaveValidationListener> saveListeners = new LinkedList<SaveValidationListener>();

    @Override
    public void addSaveValidationListener(SaveValidationListener saveValidationListener) {
        saveListeners.add(saveValidationListener);
    }

    protected void notifyAllSaveValidationListeners(SaveValidationEvent eNew) {
        for (Iterator<SaveValidationListener> it = saveListeners.iterator(); it.hasNext();) {
            SaveValidationListener saveValidatorListener = it.next();
            saveValidatorListener.saveValidationSaved(eNew);
        }
    }
    
    
    
}
